package cn.kgc.listener.rabbitmq.sms;

import cn.hutool.json.JSONUtil;
import cn.kgc.listener.AbstractMessageListener;
import cn.kgc.message.SmsMessage;
import cn.kgc.utils.SmsSenderRetry;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author YC
 */
@Slf4j
@Component
public class SmsMessageInfoListener extends AbstractMessageListener {

    @Resource
    private SmsSenderRetry smsSenderRetry;

    @Override
    public void receiveMessage(Message message, Channel channel) throws Exception {
        log.debug("模拟消息接收内容：{}", new String(message.getBody()));

        String jsonStr = new String(message.getBody());
        SmsMessage smsMessage = JSONUtil.toBean(jsonStr, SmsMessage.class);
        smsSenderRetry.sendSms(smsMessage.getPhoneNumber(), smsMessage.getCode());
    }

}
